$(document).ready(function(){
    $('#filterBlock').on('hidden', function () {
        $('#filterStatus').html("＋");
    });
    $('#filterBlock').on('shown', function () {
        $('#filterStatus').html("－");
    })
    
    // filter click handle
    $.each(["movietype","location","type"], function(index, value) {
        var isdebug = $('#debug').val();
        
        // init filter selection from url
        var filtervalue = null;
        if( isdebug ){
            filtervalue = getURLParameter(value);
        }
        else{
            filtervalue = getSearchParameter(value);
        }
        
        if( filtervalue != null && filtervalue != "null" ){
            $('#filter_ul_'+value+' > li').removeClass("active");
            $('#filter_ul_'+value+' > li[filter-value="'+filtervalue+'"]').addClass("active");
        }
        
        // bind click
        $('a[id^="filter_item_'+value+'_"]').click( function(){
            $('#filter_ul_'+value+' > li').removeClass("active");
            $(this).parent().addClass("active");
            
            advanced_search();
        });
    });
    
    getSearchParameter("type");
});

function getURLParameter(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return decodeURIComponent(pair[1]);
        }
    } 
    return null;
}

function getSearchParameter(variable) {
    var query = window.location.href;
    var queryArr = query.split("/");
    query = queryArr[queryArr.length-1];
    queryArr = query.split("?");
    if( queryArr.length > 1){
        query = queryArr[0];
    }
    var vars = query.split("-");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("_");
        if (pair[0] == variable) {
            return decodeURIComponent(pair[1]);
        }
    } 
    return null;
}

function advanced_search(){
    var isdebug = $('#debug').val();
    
    var queryString = "";
    $.each(["movietype","location","type"], function(index, value) {
        var html = $('#filter_ul_'+value+' > li.active').attr('filter-value');
        
        if( html != "全部" && html != 0 ){
            if( queryString == "" ){
                if( isdebug ){
                    queryString = value+"="+html;
                }
                else{
                    queryString = value+"_"+html;
                }
            }
            else{
                if( isdebug ){
                    queryString = queryString+"&"+value+"="+html;
                }
                else{
                    queryString = queryString+"-"+value+"_"+html;
                }
            }
        }
    });
    
    if( isdebug ){
        window.location = "index.php?action=advanced_search&"+queryString;
    }
    else{
        //加上原有的参数
        if( $("#search_input").val() != "" ){
            if( isdebug ){
                queryString = queryString+"&="+$("#search_input").val();
            }
            else{
                if(queryString == ""){
                    queryString = queryString+"q_"+encodeURIComponent( $("#search_input").val() );
                }
                else{
                    queryString = queryString+"-q_"+encodeURIComponent( $("#search_input").val() );
                }
            }
        }
        
        window.location = "/advanced_search/"+queryString;
    }
}
